package com.sharkpro.designPattern.myIterator;

import java.util.ArrayList;

/**
 * @author jianxin.wei
 * @description  具体集合
 * @date 2024/12/16 16:27
 */
public class BookShelf implements Aggregate {
    private ArrayList<Book> books;
    private int last = 0;

    // 初始化
    public BookShelf() {
        this.books = new ArrayList<>();
    }

    // 获取Book元素
    public Book getBookAt(int index) {
        return books.get(index);
    }

    // 添加Book元素
    public void appendBook(Book book) {
        books.add(book);
        last++;
    }

    // 获取元素个数
    public int getLength() {
        return last;
    }

    public MyIterator iterator() {
        BookShelfIterator bookShelfIterator = new BookShelfIterator(this);
        return bookShelfIterator;
    }

    public MyIterator iteratorPre() {
        BookShelfIteratorPre bookShelfIterator = new BookShelfIteratorPre(this);
        return bookShelfIterator;
    }

    public MyIterator iteratorTwo() {
        BookShelfIteratorTwo bookShelfIterator = new BookShelfIteratorTwo(this);
        return bookShelfIterator;
    }
}
